package com.resou.reader.bookstore;

import android.content.Context;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.ReSouApplication;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.bookstore.model.PlateNovels;
import com.resou.reader.utils.DeviceDimensionsHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_EDITOR_RECOMMEND;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_FINE;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_FINISH;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_FOR_FREE;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_HIGH_FORCE_RECOMMEND;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_HOT_SEARCH;
import static com.resou.reader.bookstore.BookStoreMainAdapter.TYPE_HOT_SERIAL;

public class BookStoreAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<PlateNovels> mPlateNovels;
    private int mType;
    private final int mDisplayWidth;
    private String platetype = "";//获取编号  判断是否是免费（55）
    private int num = 0;

    public BookStoreAdapter(Context context, int type) {
        mContext = context;
        mType = type;
        mPlateNovels = new ArrayList<>();
        mDisplayWidth = DeviceDimensionsHelper.getDisplayWidth(mContext);
    }

    public BookStoreAdapter(Context context, int type, int num) {
        mContext = context;
        mType = type;
        this.num = num;
        mPlateNovels = new ArrayList<>();
        mDisplayWidth = DeviceDimensionsHelper.getDisplayWidth(mContext);
    }

    public void setData(List<PlateNovels> plateNovels) {
        mPlateNovels = plateNovels;
        this.platetype = "";
    }

    public void setData(List<PlateNovels> plateNovels, String platetype) {
        mPlateNovels = plateNovels;
        this.platetype = platetype;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view;
        switch (mType) {
            case TYPE_EDITOR_RECOMMEND:
                view = inflater.inflate(R.layout.recycler_item_type_editor_recommend_item, viewGroup, false);
//                view.getLayoutParams().width = mDisplayWidth / 4;
                return new BookStoreHolder(view);
            case TYPE_FOR_FREE:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_for_free_item, viewGroup, false);
                return new TimeForFreeHolder(view);
//            case TYPE_TWO:
//                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_vip_zone, viewGroup, false);
//                return new BookStoreHolder(view);
            case TYPE_HOT_SEARCH:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_hot_serial_item, viewGroup, false);

                return new HotSearchHolder(view);
            case TYPE_HOT_SERIAL:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_hot_search_item, viewGroup, false);
                return new HotSerialHolder(view);
            case TYPE_FINE:
                if (num == 1) {
                    view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_high_force_recommend_item1, viewGroup, false);
                } else {
                    view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_high_force_recommend_item, viewGroup, false);
                }
                return new ForceRecommendHolder(view);
            case TYPE_FINISH:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_finish_item, viewGroup, false);
                return new FinishHolder(view);
            case TYPE_HIGH_FORCE_RECOMMEND:
                view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_best_item, viewGroup, false);
                return new BestHolder(view);
            default:
                return new DefaultHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_item_type_hot_search_item, viewGroup, false));
        }
    }

    private static final String TAG = "BookStoreAdapter";

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final PlateNovels item = mPlateNovels.get(position);

        if (viewHolder instanceof BookStoreHolder) {
            BookStoreHolder holder = (BookStoreHolder) viewHolder;
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.tvBookName.setText(item.getNovelName());

            holder.tvAuthor.setText(platetype.equals("55") ? "免费" : item.getNovelAuthor());
            if (!platetype.equals("55")) {
                holder.tvAuthor.setTextColor(((BookStoreHolder) viewHolder).tvAuthor.getResources().getColor(R.color.color_999));
            } else {
                LinearGradient mLinearGradient = new LinearGradient(0, 0, holder.tvAuthor.getPaint().getTextSize(), 0, Color.parseColor("#2BBF4E"), Color.parseColor("#47C4C0"), Shader.TileMode.CLAMP);
                holder.tvAuthor.getPaint().setShader(mLinearGradient);
            }
//            holder.tvAuthor.setTextColor(platetype.equals("55")
//                    ? ((BookStoreHolder) viewHolder).tvAuthor.getResources().getColor(R.color.color4cda64)
//                    : ((BookStoreHolder) viewHolder).tvAuthor.getResources().getColor(R.color.color_999));
            int score = (int) item.getNovelScore();
            if (score == 0) {
                score = (int) (8 + Math.random() * (10 - 8 + 1));
            }
            holder.tvScore.setText(String.valueOf(score) + "分");
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof TimeForFreeHolder) {
            TimeForFreeHolder holder = (TimeForFreeHolder) viewHolder;
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.tvBookName.setText(item.getNovelName());
            holder.tvPrice.setText(item.getOriginalPrice() / 10000 + "元");
            holder.tvPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof HotSearchHolder) {
            HotSearchHolder holder = (HotSearchHolder) viewHolder;
            holder.tvAuthor.setText(item.getNovelAuthor());
            holder.tvBookName.setText(item.getNovelName());
            holder.tvBookIntro.setText(item.getNovelSummary());
            holder.tvTag1.setVisibility(View.GONE);
            holder.tvTag2.setText(item.getNovelCategoryName());
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof HotSerialHolder) {
            HotSerialHolder holder = (HotSerialHolder) viewHolder;
            holder.tvAuthor.setText(item.getNovelAuthor());
            holder.tvBookName.setText(item.getNovelName());
            holder.tvBookIntro.setText(item.getNovelSummary());
            holder.tvTag1.setText(item.getNovelCategoryName());
            holder.tvTag2.setText(String.valueOf(item.getNovelScore()) + "分");
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof FinishHolder) {
            FinishHolder holder = (FinishHolder) viewHolder;
            holder.tvBookName.setText(item.getNovelName());
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.imageView);
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof BestHolder) {
            BestHolder holder = (BestHolder) viewHolder;
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.tvAuthor.setText(item.getNovelAuthor());
            holder.tvIntro.setText(item.getNovelSummary());
            holder.tvBookName.setText(item.getNovelName());
            holder.tvCategory.setText(item.getNovelCategoryName());
            if (num == 9) {
                LinearGradient mLinearGradient = new LinearGradient(0, 0, holder.tvCategory.getPaint().getTextSize(),0 , Color.parseColor("#FF7185"), Color.parseColor("#FFA780"), Shader.TileMode.CLAMP);
                holder.tvCategory.getPaint().setShader(mLinearGradient);
                holder.tvSeparator.setTextColor(Color.parseColor("#FF7185"));
            }
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
        if (viewHolder instanceof ForceRecommendHolder) {
            ForceRecommendHolder holder = (ForceRecommendHolder) viewHolder;
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)).load(item.getNovelCover()).into(holder.ivCover);
            holder.tvAuthor.setText(item.getNovelAuthor());
            holder.tvTag1.setText(item.getNovelCategoryName());
            holder.tvTag2.setVisibility(View.GONE);
            if (num == 1) {
                LinearGradient mLinearGradient = new LinearGradient(0, 0, holder.tvNumberOfViewers.getPaint().getTextSize(),0 , Color.parseColor("#FF7285"), Color.parseColor("#FFA87F"), Shader.TileMode.CLAMP);
                holder.tvNumberOfViewers.getPaint().setShader(mLinearGradient);
            } else {
                LinearGradient mLinearGradient = new LinearGradient(0, 0, holder.tvNumberOfViewers.getPaint().getTextSize(),0 , Color.parseColor("#D950FC"), Color.parseColor("#FB8CD0"), Shader.TileMode.CLAMP);
                holder.tvNumberOfViewers.getPaint().setShader(mLinearGradient);
            }
            holder.tvNumberOfViewers.setText(item.getNovelChaseNum() + "人在看");
            holder.tvIntro.setText(item.getNovelSummary());
            holder.tvBookName.setText(item.getNovelName());
            holder.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(mContext, item.getNovelId()));
        }
    }

    @Override
    public int getItemCount() {
        return mPlateNovels.size();
    }

    class BookStoreHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tvScore)
        TextView tvScore;

        public BookStoreHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class TimeForFreeHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        public TimeForFreeHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BestHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tv_category)
        TextView tvCategory;
        @BindView(R.id.tv_separator)
        TextView tvSeparator;
        @BindView(R.id.tv_intro)
        TextView tvIntro;

        public BestHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class HotSearchHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_book_intro)
        TextView tvBookIntro;
        @BindView(R.id.iv_author_icon)
        ImageView ivAuthorIcon;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tv_tag1)
        TextView tvTag1;
        @BindView(R.id.tv_tag2)
        TextView tvTag2;

        public HotSearchHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ForceRecommendHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.tv_intro)
        TextView tvIntro;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tv_tag1)
        TextView tvTag1;
        @BindView(R.id.tv_tag2)
        TextView tvTag2;
        @BindView(R.id.tv_number_of_viewers)
        TextView tvNumberOfViewers;

        public ForceRecommendHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class HotSerialHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.card_view)
        CardView cardView;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_book_intro)
        TextView tvBookIntro;
        @BindView(R.id.iv_author_icon)
        ImageView ivAuthorIcon;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tv_tag1)
        TextView tvTag1;
        @BindView(R.id.tv_tag2)
        TextView tvTag2;

        public HotSerialHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FinishHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_view)
        ImageView imageView;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;

        public FinishHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        public DefaultHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
